package com.shinhye.wisdompeking.fragment;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shinhye.wisdompeking.R;
import com.shinhye.wisdompeking.base.BaseDataAdapter;
import com.shinhye.wisdompeking.base.BaseFragment;

public class MenuFragment extends BaseFragment {
	private int currentPos = 0;
	private ListView lv_menu_news_center;
	private MenuAdapter adapter;

	@Override
	public View initView(LayoutInflater inflater) {
		View view = View.inflate(getActivity(), R.layout.layout_left_menu, null);
		lv_menu_news_center = (ListView) view.findViewById(R.id.lv_menu_news_center);
		return view;
	}

	@Override
	public void initData(Bundle savedInstanceState) {
		
	}
	
	public void initMenu(List<String> list_menu) {
		adapter = new MenuAdapter(getActivity(), list_menu);
		
		lv_menu_news_center.setAdapter(adapter);
		lv_menu_news_center.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				adapter.notifyDataSetChanged();
				if(currentPos != position) {
					getMainActivity().getHomeFragment().getNewsCenterPager().switchNewsCenterPager(position);
					currentPos = position;
				}
				getMenu().toggle();
			}
		});
	}
	
	class MenuAdapter extends BaseDataAdapter<String> {
		public MenuAdapter(Context context, List<String> list) {
			super(context, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null) {
				convertView = View.inflate(context, R.layout.layout_item_menu, null);
				ViewHolder holder = new ViewHolder();
				holder.iv_menu_item = (ImageView) convertView.findViewById(R.id.iv_menu_item);
				holder.tv_menu_item = (TextView) convertView.findViewById(R.id.tv_menu_item);
				convertView.setTag(holder);
			}
			ViewHolder holder = (ViewHolder) convertView.getTag();
			holder.tv_menu_item.setText(list.get(position));
			
			if(position == currentPos){
				holder.iv_menu_item.setImageResource(R.drawable.menu_arr_select);
				holder.tv_menu_item.setTextColor(context.getResources().getColor(R.color.red));
				convertView.setBackgroundResource(R.drawable.menu_item_bg_select);
			}else{
				holder.iv_menu_item.setImageResource(R.drawable.menu_arr_normal);
				holder.tv_menu_item.setTextColor(context.getResources().getColor(R.color.white));
				convertView.setBackgroundResource(R.drawable.transparent);
			}
			return convertView;
		}
	}
	
	class ViewHolder {
		ImageView iv_menu_item;
		TextView tv_menu_item;
	}
}
